const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { USER, REGION, ORDERS, WASH } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 判断是否存在用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where openid=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 添加用户
 */
function addUser(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${USER}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改用户信息
 */
function updataUser(userId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        Object.entries(data).forEach(item => {
            if (sql.toString()) {
                sql += ',';
            }
            sql += `${item[0]}="${item[1]}"`
        })
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 根据用户的id返回用户信息
 */
function GetUsersById(id) {
    return new Promise((resolve, reject) => {
        query(`select userName,userUrl,usercontent,userCountry,userSex,userId from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 返回所有澡堂信息
 */
function getRegionAll() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${REGION}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取指定澡堂
 */
function getRegionInfo(regionId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${REGION} where regionId=?`, { regionId })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 新增预约
 */
function addOrder(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ORDERS}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取预约
 */
function getOrderssById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${ORDERS} where orderId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 修改预约的状态
 */
function setOrderState(orderId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${ORDERS} SET ${sql} WHERE orderId=${orderId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 修改澡堂状态
 */
function setRegionState(regionId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${REGION} SET ${sql} WHERE regionId=${regionId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 获取用户余额
 */
function getbeginMoneyUser(userId) {
    return new Promise((resolve, reject) => {
        query(`select userMon from ${USER} where userId=?`, { userId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 用户充值
 */
function userRecharge(userId, data) {
    return new Promise((resolve, reject) => {
        let sql = `userMon=userMon+${data.Money}`;
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取个人预约信息
 */
function getOrderList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${ORDERS}.userId`]: like.userId,
            [`${ORDERS}.regionId`]: `${REGION}.regionId`
        });
        query(`select  ${sqlCount} ${ORDERS}.*,${REGION}.* from ${ORDERS},${REGION} ${condition} order by orderId desc  limit ?,${limit} `, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加洗澡记录
 */
function addWash(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${WASH}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 结束洗澡
 */
function updateWash(washId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${WASH} SET ${sql} WHERE washId=${washId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
exports.isUser = isUser;
exports.addUser = addUser;
exports.updataUser = updataUser;
exports.GetUsersById = GetUsersById;
exports.getRegionAll = getRegionAll;
exports.getRegionInfo = getRegionInfo;
exports.addOrder = addOrder;
exports.setRegionState = setRegionState;
exports.getOrderssById = getOrderssById;
exports.setOrderState = setOrderState;
exports.getbeginMoneyUser = getbeginMoneyUser;
exports.userRecharge = userRecharge;
exports.getOrderList = getOrderList;
exports.addWash = addWash;
exports.updateWash = updateWash;
